﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IChessEngine;
using ChessEngine.Basic;

namespace ChessEngine.Gaming
{
    public class ChsGameMovement : IChsGameMovement
    {
        private IChsMovement movement;
        private IChsMovementKey movementKey;
        private string pgnNotation = String.Empty;
        public Guid SourcePositionId
        {
            get { return movementKey.SourcePositionId; }
        }
        public Guid TargetPositionId
        {
            get { return movementKey.TargetPositionId; }
        }
        public IChsMovementKey MovementKey
        {
            get { return movementKey; }
        }
        public string PgnNotation
        {
            get
            {
                return pgnNotation;
            }
            set
            {
                pgnNotation = value;
            }
        }
        public Guid Id
        {
            get { return movementKey.TargetPositionId; }
        }
        public ChsGameMovement(IChsSquare _source,
                               IChsSquare _target,
                               EChsPieceType _type,
                               Guid _sourcePositionId,
                               Guid _targetPositionId)
        {
            if(_type != EChsPieceType.NONE)
                movement = ChsFactory.CreateMovement(_source, _target, _type);
            else
                movement = ChsFactory.CreateMovement(_source, _target);
            movementKey = ChsFactory.CreateMovementKey(_sourcePositionId,_targetPositionId);
        }
        public ChsGameMovement(IChsMovement _movement,
                               Guid _sourcePositionId,
                               Guid _targetPositionId)
        {
            movement = ChsFactory.CreateMovement(_movement);
            movementKey = ChsFactory.CreateMovementKey(_sourcePositionId, _targetPositionId);
        }
        public IChsSquare Source
        {
            get { return movement.Source; }
        }
        public IChsSquare Target
        {
            get { return movement.Target; }
        }
        public int DifX
        {
            get { return movement.DifX; }
        }
        public int DifY
        {
            get { return movement.DifY; }
        }
        public EChsPieceType PromotingType
        {
            get { return movement.PromotingType; }
        }
        public override string ToString()
        {
            return String.Format("({0},{1})->({2},{3})",
                                Source.X, Source.Y, Target.X, Target.Y);
        }
    }
}
